package com.rzico.sbl.ui.report.depot

import android.widget.ImageView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportDepotListBinding
import com.rzico.sbl.model.DepotManager
import com.rzico.sbl.viewmodel.ReportDepotViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.gone
import com.xinnuo.common.extend.orEmpty
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.*
import com.xinnuo.common_ui.glideExt.loadCircleImage
import com.xinnuo.slimadapter.SlimAdapter

class ReportDepotListActivity : BaseActivity(R.layout.activity_report_depot_list) {

    private val mBinding by lazy { ActivityReportDepotListBinding.bind(getRootView()) }

    override fun getViewModel() = get<ReportDepotViewModel>()

    override fun initData() {
        initTitle(title = "库管列表")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.depotBottom.gone()

        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关库管信息！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            if (!isLoadingMore) {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                        }
                    )

                    view.addItemDecoration(SpaceTBDecoration())

                    mAdapter = SlimAdapter.creator()
                        .register<DepotManager>(R.layout.item_report_depot_list) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_depot_name, bean.name.orEmpty("暂无姓名"))
                                text(R.id.item_depot_phone, bean.mobile.orEmpty("暂无电话"))
                                text(R.id.item_depot_num, bean.problem)
                                text(R.id.item_depot_out, bean.debit)
                                text(R.id.item_depot_in, bean.credit)

                                with<ImageView>(R.id.item_depot_img) {
                                    it.loadCircleImage(bean.logo, R.mipmap.default_logo3)
                                }

                                clicked {
                                    startActivityEx<ReportDepotDetailActivity>(
                                        "adminId" to bean.id
                                    )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    /**
     * 库管列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .depotList(
                page = index,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        if (it.count() > 0) pageNum++
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}